package com.hcy.orderapi.customer.controller;

import com.hcy.orderbusiness.Impl.OptOrderBzImpl;
import com.hcy.ordermodel.dto.request.CancelOrderRequestDTO;
import com.hcy.ordermodel.dto.request.CustomerOrderListRequestDTO;
import com.hcy.ordermodel.dto.request.PayOrderRequestDTO;
import com.hcy.ordermodel.dto.request.UpdateOrderOriginPointRequestDTO;
import com.hcy.ordermodel.dto.response.SimpleOrderResponseDTO;
import com.hcy.rabbitbasicmodel.enums.Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/3/24 14:50
 */
@RestController
@RequestMapping("/customer")
public class CustomerOrderController {

    Logger logger = LoggerFactory.getLogger(CustomerOrderController.class);

    @Autowired
    OptOrderBzImpl optOrderBz;

    /**
     * @Author huchenying
     * @Description 更改订单起点
     * @Date 2022/4/7 15:55
     **/
    @PostMapping("/upDateOriginPoint")
    public ResponseData<SimpleOrderResponseDTO> upDateOriginPoint(@Validated @RequestBody UpdateOrderOriginPointRequestDTO requestDTO) {
        logger.info("upDateOriginPoint start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<SimpleOrderResponseDTO> responseData = new ResponseData<>();
        try {
            SimpleOrderResponseDTO res = optOrderBz.upDateOriginPoint(requestDTO);
            responseData.setCode(Code.Success);
            responseData.setData(res);
            return responseData;
        } catch (Exception e) {
            logger.error("e -> {}", ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
            responseData.setMsg("订单起点更新失败！");
            return responseData;
        }
    }

    /**
     * @Author huchenying
     * @Description 获取订单列表
     * @Date 2022/4/7 15:55
     **/
    @PostMapping("/getOrderList")
    public ResponseData<List<SimpleOrderResponseDTO>> getOrderList(@Validated @RequestBody CustomerOrderListRequestDTO requestDTO) {
        logger.info("getOrderList start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<List<SimpleOrderResponseDTO>> responseData = new ResponseData<>();
        try {
            List<SimpleOrderResponseDTO> responseDTOList = optOrderBz.queryOrder(requestDTO);
            responseData.setCode(Code.Success);
            responseData.setData(responseDTOList);
            logger.info("res -> {}", ToStringBuilder.reflectionToString(responseData));
            return responseData;
        } catch (Exception e) {
            logger.error("e -> {}", ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
            responseData.setMsg("订单查询失败！");
            return responseData;
        }
    }

    /**
     * @Author huchenying
     * @Description 乘客取消订单
     * @Date 2022/4/7 15:55
     **/
    @PostMapping("/cancelOrder")
    public ResponseData<SimpleOrderResponseDTO> customerCancelOrder(@Validated @RequestBody CancelOrderRequestDTO requestDTO) {
        logger.info("cancelOrder start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<SimpleOrderResponseDTO> responseData = new ResponseData<>();
        try {
            SimpleOrderResponseDTO simpleOrderResponseDTO = optOrderBz.customerCancelOrder(requestDTO.getOrderNumber());
            responseData.setCode(Code.Success);
            responseData.setData(simpleOrderResponseDTO);
            logger.info("res -> {}", ToStringBuilder.reflectionToString(responseData));
            return responseData;
        } catch (Exception e) {
            logger.error("e -> {}", ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
            responseData.setMsg("取消订单失败！");
            return responseData;
        }
    }
    /**
     * @Author huchenying
     * @Description payAfter
     * @Date 2022/4/7 13:15
     **/
    @PostMapping("/payOrderByCoin")
    public ResponseData<SimpleOrderResponseDTO> payOrderByCoin(@Validated @RequestBody PayOrderRequestDTO requestDTO) {
        logger.info("payOrderByCoin start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<SimpleOrderResponseDTO> responseData = new ResponseData<>();
        try {
            SimpleOrderResponseDTO responseDTO = optOrderBz.payOrder(requestDTO);
            responseData.setData(responseDTO);
            responseData.setCode(Code.Success);
        } catch (Exception e) {
            logger.error("余额支付异常 e -> {}", ToStringBuilder.reflectionToString(e));
            responseData.setCode(Code.Fail);
            responseData.setMsg("支付失败！");
        }
        logger.info("res -> {}", ToStringBuilder.reflectionToString(responseData));
        return responseData;
    }
}
